/* Copyright [2023] <gyl> */
#pragma once

#include <string>
#include <vector>

#include "common/rc.h"

static int INSERT_BIT = 0;
static int DELETE_BIT = 1;
static int UPDATE_BIT = 2;
static int SELECT_BIT = 3;
static int DROP_BIT = 4;
static int GRANT_BIT = 5;

class SQLStageEvent;
class Stmt;
class UseStmt;
class SelectStmt;
class FilterStmt;
class InsertStmt;
class DeleteStmt;
class UpdateStmt;
class DropTableStmt;
class GrantStmt;
class LogicalOperator;
class Db;

/**
 * @brief 进行SQL语句鉴权
 * @ingroup SQLStage
 */
class IdentifyStage {
 public:
  RC handle_request(SQLStageEvent *sql_event);
  static RC get_all_users(Db *db, std::vector<std::string> &users);
  static RC get_priv(Db *db, std::string user, std::string table_name,
                     int &priv);

 private:
  RC identify_priv(UseStmt *use_stmt, const std::string &user);
  RC identify_priv(SelectStmt *select_stmt, const std::string &user);
  RC identify_priv(InsertStmt *insert_stmt, std::string user);
  RC identify_priv(DeleteStmt *delete_stmt, std::string user);
  RC identify_priv(UpdateStmt *update_stmt, std::string user);
  RC identify_priv(DropTableStmt *create_stmt, std::string user);
  RC identify_priv(SQLStageEvent *sql_event);
};
